//
//  CZCommentViewCell.swift
//  GZWeibo007
//
//  Created by apple on 15/12/24.
//  Copyright © 2015年 Apple. All rights reserved.
//
//

import UIKit
//自定义Cell
class CZCommentViewCell: UITableViewCell {

    var comment: CZComment? {
        didSet {
            //显示头像
          if let profileImage = comment?.user?.profile_image_url {
                let url = NSURL(string: profileImage)!
                iconView.sd_setImageWithURL(url, placeholderImage: UIImage(named: "avatar"))
            }
            //显示用户名
            userName.text = comment?.user?.screen_name
            //显示时间
            timeLabel.text = comment?.created_at
            //显示评论内容
            commentLaber.text = comment?.text
        }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        prepareUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //添加UI控件
    func prepareUI() {
        contentView.addSubview(iconView)
        contentView.addSubview(userName)
        contentView.addSubview(timeLabel)
        contentView.addSubview(commentLaber)
        
        iconView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: 30, height: 30), offset: CGPoint(x: 8, y: 8))
        userName.ff_AlignHorizontal(type: ff_AlignType.TopRight, referView: iconView, size: nil, offset: CGPoint(x: 8, y: 0))
        timeLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: userName, size: nil, offset: CGPoint(x: 0, y: 8))
        commentLaber.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: timeLabel, size: nil, offset: CGPoint(x: 0, y: 8))
        contentView.addConstraint(NSLayoutConstraint(item: commentLaber, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -2 * 8))
        contentView.addConstraint(NSLayoutConstraint(item:contentView , attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: commentLaber, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 8))
    }
    

    //MARK: - 懒加载
    //用户头像
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "avatar"))
    
    //用户名称
    private lazy var userName: UILabel = {
        let laber = UILabel()
        laber.font = UIFont.systemFontOfSize(12)
        
        return laber
    }()
    
    //时间
    private lazy var timeLabel: UILabel = {
        let laber = UILabel()
        laber.font = UIFont.systemFontOfSize(9)
        laber.textColor = UIColor.orangeColor()
        return laber
    }()
    
    //评论文字
    private lazy var commentLaber: UILabel = {
        let laber = UILabel()
        laber.font = UIFont.systemFontOfSize(14)
        laber.numberOfLines = 0
        return laber
    }()

}
